package cn.gene.timeyan.article.webapi.service.impl;

import cn.gene.timeyan.article.service.IShareService1;
import cn.gene.timeyan.article.webapi.mapper.ArticleMapper;
import cn.gene.timeyan.article.webapi.mapper.ShareMapper;
import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.pojo.article.model.Article;
import cn.gene.timeyan.pojo.article.model.Share;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@DubboService
@Service
public class ShareServiceImpl1 implements IShareService1 {

    @Autowired
    ShareMapper shareMapper;

    @Autowired
    ArticleMapper articleMapper;

    /**
     * 增加分享记录
     * @param userId
     * @param articleId
     * @param moduleId
     * @return
     */
    @Override
    public int addShareService(Long userId, Long articleId, Long moduleId) {

//        System.out.println("开始塞你剪贴板里");
//        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
//        Transferable tText = new StringSelection(url);
//        clip.setContents(tText, null);
//        System.out.println("完成粘贴到你剪贴板操作");

        Article article = articleMapper.getArticleByArticleId(articleId);
        if (article == null){
            throw new TimeyanServiceException(ResponseCode.BAD_REQUEST,
                    "找不到分享的帖子（" + ResponseCode.BAD_REQUEST.getValue() + "），请稍后再次尝试！");

        }
        //拿到分享数
        int articleRetweetedarticleRetweeted  = article.getArticleRetweetedarticleRetweeted();
        System.out.println(articleRetweetedarticleRetweeted);
        //分享数+1
        articleRetweetedarticleRetweeted = articleRetweetedarticleRetweeted + 1;
        System.out.println("**************");
        System.out.println(articleRetweetedarticleRetweeted);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        int row1  = articleMapper.updateArticleRetweetedarticleRetweeted(articleId,date,articleRetweetedarticleRetweeted);
        if (row1!= 1){
            throw new TimeyanServiceException(ResponseCode.INTERNAL_SERVER_ERROR,
                    "更新失败,服务器忙（" + ResponseCode.INTERNAL_SERVER_ERROR.getValue() + "），请稍后再次尝试！");

        }
        Share share = new Share();
        share.setUserId(userId);
        share.setArticleId(articleId);
        share.setModuleId(moduleId);
        share.setPlatform("-分享去哪呢 -我不道啊");

        share.setCreateTime(date);
        share.setShareTime(date);
        share.setUpdateTime(date);
       int row = shareMapper.insertShare(share);

        if (row!= 1){
            throw new TimeyanServiceException(ResponseCode.INTERNAL_SERVER_ERROR,
                    "添加失败,服务器忙（" + ResponseCode.INTERNAL_SERVER_ERROR.getValue() + "），请稍后再次尝试！");

        }
        return row;
    }

}
